package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {

    @Autowired
    TmsService tmsService;

    @RequestMapping("/showTotal")
    public Object showTotal(String nowDate){
        List<Map> maps = tmsService.showTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",maps.get(0).get("sum"));
        return jsonObject;
    }

    @RequestMapping("/showPercent")
    public Object showPercent(String nowDate){
        List<Map> maps = tmsService.showPercent(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");


        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","series1");

        JSONArray data2Array = new JSONArray();
        for (Map map : maps) {
            JSONObject data2Object = new JSONObject();
            data2Object.put("name",map.get("name"));
            data2Object.put("value",map.get("zb"));
            data2Array.add(data2Object);

        }
        serObject.put("data",data2Array);
        serArray.add(serObject);
        JSONObject dataObject = new JSONObject();
        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/showOrder")
    public Object showOrder(String nowDate){
        List<Map> maps = tmsService.showOrder(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();
        JSONArray cateArray = new JSONArray();
        for (Map map : maps) {
            cateArray.add(map.get("orgName"));
        }

        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        JSONArray data2Array = new JSONArray();
        for (Map map : maps) {
            data2Array.add(map.get("count"));
        }
        serObject.put("name","地区数量");
        serObject.put("data",data2Array);
        serArray.add(serObject);

        dataObject.put("categories",cateArray);
        dataObject.put("series",serArray);

        jsonObject.put("data",dataObject);

        return jsonObject;
    }


    @RequestMapping("/showCount")
    public Object showCount(String nowDate){
        List<Map> maps = tmsService.showCount(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");


        JSONObject dataObject = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : maps) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("name"));
            mapObject.put("value",map.get("count"));
            mapArray.add(mapObject);
        }
        dataObject.put("mapData",mapArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
